<!--供应商-->
<template>
  <div>
    <el-select v-model="supplierId" :disabled="disabled" :placeholder="placeholder" clearable filterable
               style="width: 100%;"
               @change="supplierChange">
      <el-option v-for="item in supplierList" :key="item.supplierId" :label="item.supplierName" :value="item.supplierId">
      </el-option>
    </el-select>
  </div>
</template>

<script lang="ts" setup>

import {listMaterialSupplier} from '@/api/material/materialSupplier'
import {useUserStoreHook} from "@/store/modules/user";

const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;

// 厂商list
const supplierList = ref<any>([])
const supplierId = ref("")
const emits = defineEmits(["supplierChange"])
const props = defineProps({
  placeholder: {
    type: String,
    default: "请选择"
  },
  disabled: {
    type: Boolean,
    default: false
  },
  // 厂商id
  supplierId: {
    type: String,
    default: ''
  }
})
getSupplierList()

watch(() => props.supplierId, (newValue, _oldValue) => {
  supplierId.value = newValue
}, {deep: true, immediate: true})

// 获取厂商list
function getSupplierList() {
  const param = {delFlag: '0', supplierClass: '1', usedFlag: 'Y',orgId: userInfo.orgId}
  listMaterialSupplier(param).then(res => {
    supplierList.value = res.data
  })
}

function supplierChange(value) {
  let supplierInfo = {}
  let index = supplierList.value.findIndex((item: any) => {
    return item.supplierId === value
  })
  if (index > -1) {
    supplierInfo = supplierList.value[index]
  }
  emits("supplierChange", supplierInfo)
}
</script>

<script lang="ts">
export default {
  name: "supplier"
}
</script>

<style scoped></style>
